﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using IOEx;
using System.Security.Cryptography;

namespace QuanLyTruong.Business
{
    class General
    {
        public static string GenerateMakhoitao(string CDKey)
        {
            string newcode = "";
            DriveListEx m_list;
            m_list = new DriveListEx();
            m_list.Load();
            if (m_list.Count > 0)
            {
                string newRawKey = (m_list[0].SerialNumber + CDKey + CDKey + CDKey);
                string algKey = m_list[0].SerialNumber.Substring(0, 8);
                newRawKey = General.Encrypt(newRawKey, newRawKey, true);
                if (newRawKey.Length > 32)
                    newRawKey = newRawKey.Substring(0, 32);
                else if (newRawKey.Length < 32)
                {
                    newRawKey = newRawKey + newRawKey + newRawKey + newRawKey + newRawKey;
                    newRawKey = newRawKey.Substring(0, 32);
                }
                for (int i = 0; i < newRawKey.Length; i = i + 2)
                {
                    int charnumber = (int)newRawKey[i] + (int)newRawKey[i + 1];
                    int oNumber = 0;
                    General.GetOneDigigNumberFromOtherNumber(charnumber, out oNumber);
                    newcode += oNumber.ToString();
                }
            }
            return newcode;
        }

        public static void GetOneDigigNumberFromOtherNumber(int iNumber, out int oNumber)
        {
            if (iNumber.ToString().Length > 1)
            {
                int temp = 0;
                for (int i = 0; i < iNumber.ToString().Length; i++)
                {
                    temp += Convert.ToInt32(iNumber.ToString()[i].ToString());
                }
                oNumber = temp;
                GetOneDigigNumberFromOtherNumber(oNumber, out oNumber);
            }
            else
            {
                oNumber = iNumber;
                return;
            }
        }

        public static string GenerateActiveNumber(string ActiveKeyRaw, string CDKey)
        {
            string ActiveKey = CDKey + ActiveKeyRaw + "+-*:";
            string newcode = "";
            for (int i = 0; i < ActiveKey.Length; i = i + 2)
            {
                int charnumber = (int)ActiveKey[i] + (int)ActiveKey[i + 1];
                int oNumber = 0;
                GetOneDigigNumberFromOtherNumber(charnumber, out oNumber);
                newcode += oNumber.ToString();
            }
            return newcode;
        }

        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

    }
}
